import unittest
import random
from kea2 import precondition, prob
from hypium import BY, UiDriver

class myDevCase(unittest.TestCase):

    d: "UiDriver"

    search_keywords = [
        "UI布局",
        "动效",
        "三方库",
        "Native",
        "性能示例",
        "其他"
    ]

    @prob(0.8)
    @precondition(
        lambda self: self.d.find_component(BY.type("SearchField"))
        and self.d.find_component(BY.text("搜索指定内容"))
    )
    def test_search_random_keyword(self):
        if not self.search_keywords:
            print("关键词列表为空")
            return
        print("搜索框 存在")
        comp = self.d.find_component(BY.type("SearchField"))
        comp.clearText()
        keyword = random.choice(self.search_keywords)
        print(f"随机选择关键词: {keyword}，并输入搜索框")
        comp.inputText(keyword)
        print("点击搜索")
        self.d.find_component(BY.text("搜索")).click()

    @prob(0.8)
    @precondition(
        lambda self: self.d.find_component(BY.text("多媒体发布"))
    )
    def test_click_for_you(self):
        print("多媒体发布 存在")
        comp = self.d.find_component(BY.text("For You"))
        print("For You 存在")
        comp.click()
        print("点击 For You")
        assert self.d.find_component(BY.text("仅演示，可自行实现业务功能")) is None

    @prob(0.8)
    @precondition(
        lambda self: self.d.find_component(BY.text("多媒体发布"))
    )
    def test_click_Discover(self):
        print("多媒体发布 存在")
        comp = self.d.find_component(BY.text("Discover"))
        print("Discover 存在")
        comp.click()
        print("点击 Discover")
        assert self.d.find_component(BY.text("仅演示，可自行实现业务功能")) is None

    @prob(0.8)
    @precondition(
        lambda self: self.d.find_component(BY.text("关注"))
    )
    def test_click_attention_button(self):
        print("关注 按钮存在")
        comp = self.d.find_component(BY.text("关注"))
        print("点击: 关注")
        comp.click()
        assert self.d.find_component(BY.text("仅演示，可自行实现业务功能")) is None

    @prob(0.8)
    @precondition(
        lambda self: self.d.find_component(BY.text("说点什么"))
    )
    def test_click_say_something(self):
        print("说点什么 按钮存在")
        comp = self.d.find_component(BY.text("说点什么"))
        print("点击: 说点什么")
        comp.click()
        assert self.d.find_component(BY.text("仅演示，可自行实现业务功能")) is None


if __name__ == "__main__":
    d = UiDriver.connect(
        device_sn = "4ABVB25114010220",
        connector="hdc"
    )
